﻿// ----------------------------------------------------------------------------------------
// Author:                    Magaly Vargas y Joaquín López
// Company:                   ALBALIA INTERACTIVA S.L.
// Assembly version:          1.0.0.0
// Date:                      22/02/2010
// Time:                      12:00
// Solution Name:             OffInvoiceWordTemplate
// Solution Filename:         OffInvoiceWordTemplate
// Solution FullFilename:     C:\Desarrollo\OffInvoiceWordTemplate
// Project Name:              OffInvoiceWordTemplate
// Project Filename:          OffInvoiceWordTemplate.csproj
// Project FullFilename:      C:\Desarrollo\OffInvoiceWordTemplate\OffInvoiceWordTemplate.csproj
// Project Item Name:         FieldFormats.cs
// Project Item Filename:     FieldFormats.cs
// Project Item FullFilename: C:\Desarrollo\OffInvoiceWordTemplate\FieldFormats.cs
// Project Item Kind:         Código
// Purpose:                   
//Copyright © 2009 - 2010 Albalia Interactiva S.L. Reservados todos los derechos.
// ----------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Windows.Forms;
using System.Xml;

namespace OffInvoiceWordTemplate
{
    public class FieldFormats
    {
        string language;
        string languajePath;
        public string messagePath;
        CultureInfo culture;
        public NumberFormatInfo NumberFormat{get;set;}

        public FieldFormats(CultureInfo culture, NumberFormatInfo num, string language)
        {
            this.culture = culture;
            this.NumberFormat = num;
            this.language = language;           

            languajePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Microsoft OffInvoice\Languages\" + language + ".xml";
            messagePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Microsoft OffInvoice\Messages\" + language + ".xml";
                        
        }

        public string txtDescription_Validating(string description)
        {
            if (string.IsNullOrEmpty(description))
            {
                throw new Exception("ErrorMandatoryDescription");    
            }

            return description;
        }

        public string  txtUd_Validating(string units)
        {
            try
            {
                double mVar = double.Parse(units, NumberFormat);
                units = string.Format(NumberFormat, "{0:#,0.00}", mVar);
            }
            catch (Exception) 
            {
                throw new Exception("ErrorUnlegalLiteralsUd");            
            }

            return units;
        }
  
        public string txtDiscount_Validating(string disc)
        {
            try
            {
                if (!string.IsNullOrEmpty(disc))
                {
                    double xVar = double.Parse(disc, NumberFormat);
                    if (xVar < 0)
                    {
                        throw new Exception("negativeDiscountRate");
                    }
                    else
                    {
                        disc = string.Format(NumberFormat, "{0:#,0.00}", xVar);
                    }
                }               
            }
            catch (Exception ex)
            {
                if (ex.Message.Equals("negativeDiscountRate"))
                {
                    throw new Exception("ErrorNegativeDiscount");
                }
                else
                {
                    throw new Exception("ErrorUnlegalLiteralsDesc");      
                }
            }

            return disc;
        }

        public string txtPrice_Validating(string price)
        {
            try
            {
                double xVar = double.Parse(price, NumberFormat);
                price = string.Format(NumberFormat, "{0:#,0.00}", xVar);
            }
            catch (Exception)
            {
                throw new Exception("ErrorUnlegalLiteralsPrice");

            }

            return price;
        }

        public string txtExchangeRate(string extangeRate)
        {
            try
            {
                double xVar = double.Parse(extangeRate, NumberFormat);
                extangeRate = string.Format(NumberFormat, "{0:#,0.00}", xVar);
            }
            catch (Exception)
            {
                throw new Exception("ErrorUnlegalLiteralsExchangeRate");

            }

            return extangeRate;
        }

        public string cbxIVA_Validating(string IVA)
        {
            try
            {
                double xVar = double.Parse(IVA, NumberFormat);
                IVA = string.Format(NumberFormat, "{0:#,0.00}", xVar);

            }
            catch (Exception)
            {

                throw new Exception("ErrorUnlegalLiteralsIVA");

            }

            return IVA;
        }

        public string txtIRPF_Validating(string IRPF)
        {
            try
            {
                double xVar = double.Parse(IRPF, NumberFormat);
                IRPF = string.Format(NumberFormat, "{0:#,0.00}", xVar);
            }
            catch (Exception)
            {

                throw new Exception("ErrorUnlegalLiteralsIRPF");

            }

            return IRPF;
        }

        public string LoadLabel(string path, string tag)
        {
            string label = " ";
            try
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(path);
                string lb = xmldoc.GetElementsByTagName(tag).Item(0).InnerText;
                if (!string.IsNullOrEmpty(lb))
                    label = lb;

                return label;

            }
            catch (Exception)
            {
                return label;
            }
        }

        public string twoDecimalFormat(string value)
        {
            try
            {
                double xVar = double.Parse(value, NumberFormat);
                value = string.Format(NumberFormat, "{0:#,0.00}", xVar);
            }
            catch (Exception)
            {           

            }

            return value;
        }

    }
}
